<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
class Index extends Base
{
    public function systemSet()
    {
        if(input("param.zh_cn")=='en-us'){
            $data=Db::name('system_set')->alias('a')->join('system_set_en b', 'a.id = b.system_set_id','LEFT')->field('a.id,a.logo,a.tel,a.qrcode,a.qrcode_wechat,a.tel_phone,a.email,b.copyright,b.icp,b.address,b.technical_support,a.uptime')->where('a.id',1)->find();
        }elseif(input("param.zh_cn")=='zh-cn'){
            $data=Db::name('system_set')->where('id',1)->find();
        }else{
            return json(['code' => 501, 'msg' => '请选择语言']);
        }
        if($data){
            return json(['code' => 200, 'msg' => '成功','data'=>$data]);exit();
        }else{
            return json(['code' => 500, 'msg' => '查无数据']);exit();
        }
    }
  	//轮播图
	public function banner(){
        $list=Db::name('banner')->where('state',1)->order('sort desc')->select();
        if($list){
            return json(['code' => 200, 'msg' => '查询成功' ,'data' =>$list]);
        }else{
            return json(['code' => 500, 'msg' => '查无结果']);
        }
	}
    public function product()
    {
        if(input("param.zh_cn")=='en-us'){
            $data=Db::name('product')->alias('a')->join('product_en b', 'a.id = b.product_id','LEFT')->field('a.id,a.img,b.title,b.intro,b.introduce')->where('a.state',1)->where('a.recommend',1)->order('a.sort desc')->select();
        }elseif(input("param.zh_cn")=='zh-cn'){
            $data=Db::name('product')->field('id,img,title,intro,introduce')->where('state',1)->where('recommend',1)->order('sort desc')->select();
        }else{
            return json(['code' => 501, 'msg' => '请选择语言']);
        }
        if($data){
            return json(['code' => 200, 'msg' => '成功','data'=>$data]);exit();
        }else{
            return json(['code' => 500, 'msg' => '查无数据']);exit();
        }
    }
    public function cate()
    {
        $f_id=0;
        $map = [
            ['state', '=', 1]
        ];
        if ($f_id) {
            array_push($map,['f_id','=',$f_id]);
        }else{
            array_push($map,['f_id','=',0]);
        }
        if(input("param.zh_cn")=='zh-cn'){
            $data=Db::name('product_cate')->field('id,f_id,icon,cate_name,cate_intro')->where($map)->select();
            $title=Db::name('product_recommend_title')->where('id',1)->find();
        }elseif(input("param.zh_cn")=='en-us'){
            $data=Db::name('product_cate')->field('id,f_id,icon,cate_name_en as cate_name,cate_intro_en as cate_intro')->where($map)->select();
            $title=Db::name('product_recommend_title')->alias('a')->join('product_recommend_title_en b', 'a.id = b.title_id','LEFT')->field('a.id,b.title')->where('a.id',1)->find();
        }else{
            return json(['code' => 501, 'msg' => '请选择语言']);
        }
        if($data){
            return json(['code' => 200, 'msg' => '成功','title' => $title['title'],'data'=>$data]);exit();
        }else{
            return json(['code' => 500, 'msg' => '查无数据']);exit();
        }
    }
    public function products()
    {
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 501, 'msg' => '当前分页数']);exit();
        }
        if(!empty(input("param.c_o_id"))){
            $c_o_id=input("param.c_o_id");
        }else{
            return json(['code' => 502, 'msg' => '分类id']);exit();
        }
        if(input("param.zh_cn")=='en-us'){
            $data=Db::name('product')->alias('a')->join('product_en b', 'a.id = b.product_id','LEFT')->where('a.c_o_id',$c_o_id)->where('a.state',1)->field('a.id,a.c_o_id,b.title,a.img')->order('a.id desc')->limit(12*($page-1),12)->select();
            $count=Db::name('product')->alias('a')->join('product_en b', 'a.id = b.product_id','LEFT')->where('a.c_o_id',$c_o_id)->where('a.state',1)->field('a.id,a.c_o_id,b.title,a.img')->count();
        }elseif(input("param.zh_cn")=='zh-cn'){
            $data=Db::name('product')->where('c_o_id',$c_o_id)->where('state',1)->field('id,c_o_id,title,img')->order('id desc')->limit(12*($page-1),12)->select();
            $count=Db::name('product')->where('c_o_id',$c_o_id)->where('state',1)->field('id,c_o_id,title,img')->count();
        }else{
            return json(['code' => 503, 'msg' => '请选择语言']);
        }
        if($data){
            return json(['code' => 200, 'msg' => '成功','page'=>$page,'pages'=>ceil($count/12),'data'=>$data]);exit();
        }else{
            return json(['code' => 500, 'msg' => '查无数据']);exit();
        }
    }
    public function news()
    {
        
        if(input("param.zh_cn")=='en-us'){
            $data=Db::name('news')->alias('a')->join('news_en b', 'a.id = b.news_id','LEFT')->where('a.state',1)->field('a.id,b.title,b.detail,a.img,a.time')->order('a.id desc')->limit(0,5)->select();
        }elseif(input("param.zh_cn")=='zh-cn'){
            $data=Db::name('news')->where('state',1)->field('id,title,detail,img,time')->order('id desc')->limit(0,5)->select();
        }else{
            return json(['code' => 502, 'msg' => '请选择语言']);
        }
        if($data){
            foreach ($data as $key => $value) {
                $data[$key]['detail']=mb_substr(strip_tags($data[$key]['detail']),0,32,'utf-8');
                $data[$key]['time'] = date('Y-m-d H:i:s',$data[$key]['time']);
            }
            return json(['code' => 200, 'msg' => '成功','data'=>$data]);
        }else{
            return json(['code' => 500, 'msg' => '查无数据']);
        }
    }
    //产品搜索
    public function productSearch(){
        if(!empty(input("param.keyword"))){
            $keyword=input('param.keyword');
        }else{
            return json(['code' => 501, 'msg' => '搜索关键词呢']);
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 502, 'msg' => '当前分页数']);
        }
        $map = [
            ['state', '=', 1]
        ];
        $orderby = 'sort desc';
        if(input("param.zh_cn")=='en-us'){
            $products=Db::name('product_en')->alias('a')->join('product b', 'b.id = a.product_id','LEFT')->field('a.product_id as id,b.img,a.title,a.intro,a.introduce')->where('b.state',1)->where('a.title','like',"%{$keyword}%")->order('b.sort desc')->limit(10*($page-1),10)->select();
            $count=Db::name('product_en')->alias('a')->join('product b', 'b.id = a.product_id','LEFT')->field('a.product_id as id,b.img,a.title,a.intro,a.introduce')->where('b.state',1)->where('a.title','like',"%{$keyword}%")->count();
        }elseif(input("param.zh_cn")=='zh-cn'){
            $products=Db::name('product')->where($map)->where('title','like',"%{$keyword}%")->field('id,title,img,intro,introduce')->order($orderby)->limit(10*($page-1),10)->select();
            $count=Db::name('product')->where($map)->where('title','like',"%{$keyword}%")->field('id,title,img,intro,introduce')->count();
        }else{
            return json(['code' => 503, 'msg' => '请选择语言']);
        }
        if($products){
            return json(['code' => 200, 'msg' => '成功！','page'=>$page,'pages'=>ceil($count/10),'keyword'=>$keyword,'data'=>$products]);
        }else{
            return json(['code' => 500, 'msg' => '查无数据！']);
        }
    }
    
    
}